package org.example.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Jandmla
 * Date: 2022/8/3
 * Time: 14:31
 * Description:
 */
@Service
public class PaymentService {

    public String paymentInfo_OK(Long id){
        return "线程池：" + Thread.currentThread().getName() +
                " paymentInfo_OK, id: " + id +
                "\t"+
                "O(∩_∩)O哈哈~";
    }

    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
    })
    public String paymentInfo_TimeOut(Long id){
        int timeNumber = 3;
//        int a = 10/0;
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return "线程池：" + Thread.currentThread().getName() +
                " paymentInfo_TimeOut, id: " + id +
                "\t"+
                "O(∩_∩)O哈哈~" +
                "耗时（秒）：" + timeNumber;
    }

    public String paymentInfo_TimeOutHandler(Long id){
        return "线程池：" + Thread.currentThread().getName() +
                " 系统繁忙或出现问题，请稍后再试, id: " + id +
                "\t"+
                "o(╥﹏╥)o" ;
    }
}